/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralload;

import org.openaecfm.model.structuralload.BoundaryCondition;
import org.openaecfm.model.structuralload.BoundaryEdgeCondition;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.model.measure.Boolean;

public final class BoundaryEdgeConditionIO extends
		AbstractEntityInstanceInputImpl<BoundaryEdgeCondition> implements
		IEntityInstanceOutput<BoundaryEdgeCondition> {

	private IEntityInstanceUtilInput<BoundaryCondition> utilInput;
	private IEntityInstanceUtilOutput<BoundaryCondition> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<BoundaryCondition> utilOutput) {
		this.utilOutput = utilOutput;
	}

	private Parameter toParameter(Object object) {
		if (object instanceof Boolean) {
			return new EnumerationParameter((Boolean) object);
		} else if (object instanceof Double) {
			return new RealParameter((Double) object);
		} else {
			return DollarParameter.getInstance();
		}
	}

	private Object fromParameter(Parameter p) {
		if (p.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) p;
			return Boolean.valueOf(e.toEnumString());
		} else if (p.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) p;
			return r.toDouble();
		} else {
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(BoundaryEdgeCondition pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCBOUNDARYEDGECONDITION", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set TranslationalStiffnessByLengthX
		ei.addParameter(toParameter(pojo.getTranslationalStiffnessByLengthX()));

		// Set TranslationalStiffnessByLengthY
		ei.addParameter(toParameter(pojo.getTranslationalStiffnessByLengthY()));

		// Set TranslationalStiffnessByLengthZ
		ei.addParameter(toParameter(pojo.getTranslationalStiffnessByLengthZ()));

		// Set RotationalStiffnessByLengthX
		ei.addParameter(toParameter(pojo.getRotationalStiffnessByLengthX()));

		// Set RotationalStiffnessByLengthY
		ei.addParameter(toParameter(pojo.getRotationalStiffnessByLengthY()));

		// Set RotationalStiffnessByLengthZ
		ei.addParameter(toParameter(pojo.getRotationalStiffnessByLengthZ()));

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected BoundaryEdgeCondition doCreateObject() {
		return new BoundaryEdgeCondition();
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<BoundaryCondition> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			BoundaryEdgeCondition pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TranslationalStiffnessByLengthX
		pojo.setTranslationalStiffnessByLengthX(fromParameter(sei
				.getParameter(1)));

		// Set TranslationalStiffnessByLengthY
		pojo.setTranslationalStiffnessByLengthY(fromParameter(sei
				.getParameter(2)));

		// Set TranslationalStiffnessByLengthZ
		pojo.setTranslationalStiffnessByLengthZ(fromParameter(sei
				.getParameter(3)));

		// Set RotationalStiffnessByLengthX
		pojo.setRotationalStiffnessByLengthX(fromParameter(sei.getParameter(4)));

		// Set RotationalStiffnessByLengthY
		pojo.setRotationalStiffnessByLengthY(fromParameter(sei.getParameter(5)));

		// Set RotationalStiffnessByLengthZ
		pojo.setRotationalStiffnessByLengthZ(fromParameter(sei.getParameter(6)));
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			BoundaryEdgeCondition pojo, IAppDataManager appDataManager) {
		return ;
	}
}
